/*******************************************************************************
 * Copyright (c) 2009 L.Carbonnaux.
 *******************************************************************************/
package org.lcx.taskvision.ui.editor;

import java.util.Iterator;
import java.util.Set;

import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.ui.editors.AbstractAttributeEditor;
import org.eclipse.mylyn.tasks.ui.editors.AbstractTaskEditorPage;
import org.eclipse.mylyn.tasks.ui.editors.AttributeEditorFactory;
import org.eclipse.mylyn.tasks.ui.editors.TaskEditor;
import org.eclipse.mylyn.tasks.ui.editors.TaskEditorPartDescriptor;
import org.lcx.taskvision.core.TaskVisionCorePlugin;

/**
 * @author Laurent Carbonnaux
 */
public class TaskVisionTaskEditorPage extends AbstractTaskEditorPage {

//	private boolean isSprintCreationError=false;
//
//	public boolean isSprintCreationError() {
//		return isSprintCreationError;
//	}
//
//	public void setSprintCreationError(boolean isSprintCreationError) {
//		this.isSprintCreationError = isSprintCreationError;
//	}

	public TaskVisionTaskEditorPage(TaskEditor editor, String connector) {
		super(editor, connector);
		setNeedsSubmitButton(true);
	}

	@Override
	protected Set<TaskEditorPartDescriptor> createPartDescriptors() {
		Set<TaskEditorPartDescriptor> descriptors = super.createPartDescriptors();
		// remove unnecessary default editor parts
		for (Iterator<TaskEditorPartDescriptor> it = descriptors.iterator(); it.hasNext();) {
			TaskEditorPartDescriptor taskEditorPartDescriptor = it.next();
			if (taskEditorPartDescriptor.getId().equals(ID_PART_ATTACHMENTS)
					|| taskEditorPartDescriptor.getId().equals(ID_PART_COMMENTS)
					|| taskEditorPartDescriptor.getId().equals(ID_PART_NEW_COMMENT)
					|| taskEditorPartDescriptor.getId().equals(ID_PART_PLANNING)) {
				it.remove();
			}
			if (taskEditorPartDescriptor.getId().equals(ID_PART_PEOPLE)) {
				if(!getConnectorKind().equals(TaskVisionCorePlugin.CONNECTOR_KIND)) it.remove();
			}

		}
		return descriptors;
	}

	@Override
	protected void createParts() {
		super.createParts();
		getPart(ID_PART_ATTRIBUTES).setExpandVertically(true);
	}

	@Override
	protected AttributeEditorFactory createAttributeEditorFactory() {
		AttributeEditorFactory factory = new AttributeEditorFactory(getModel(), getTaskRepository()) {
			@Override
			public AbstractAttributeEditor createEditor(String type, TaskAttribute taskAttribute) {
				return super.createEditor(type, taskAttribute);
			}
		};
		return factory;
	}
	
//	@Override
//	public void doSubmit() {
////		ITask task = this.getTask();
////		setSprintCreationError(false);
////		if(getModel().getTaskData().isNew()) {
//////			createNewSprint(task.getSummary());
////			this.getTaskRepository();
////			//set new sprint feeds url
////		}
////		if(!this.isSprintCreationError()) {
//			super.doSubmit();
////		}
//		
//
//	}

//	private void createNewSprint(final String sprintName) {
//		
//		IRunnableWithProgress updateRunnable = new IRunnableWithProgress() {
//			public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
//				if (monitor == null) {
//					monitor = new NullProgressMonitor();
//				}
//				try {
//					monitor.beginTask(Messages.ScrumVisionTaskEditorPage_creating_sprint, IProgressMonitor.UNKNOWN);
//					ScrumVisionUiTools.createNewSprint(getTaskRepository(), monitor, sprintName);
//				} catch (final Exception e) {
//					e.printStackTrace();
//					setSprintCreationError(true);
//					throw new InvocationTargetException(e);
//				} finally {
//					monitor.done();
//				}
//			}
//		};
//
//		if (runUpdate(updateRunnable)) return;
//	}
//	
//	private boolean runUpdate(IRunnableWithProgress updateRunnable) {
//		try {
//				IProgressService service = PlatformUI.getWorkbench().getProgressService();
//				service.busyCursorWhile(updateRunnable);
//		} catch (Exception e) {
//			displayError(e);
//			return true;
//
//		}
//		return false;
//	}
//	
//	private void displayError(Throwable e) {
//		IStatus status;
//			status = new Status(IStatus.ERROR, ScrumVisionCorePlugin.PLUGIN_ID, 	e.getMessage());
//		StatusHandler.fail(status);
//	}
	
}
